<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="收款单位" prop="payoutFrom">
        <el-input
          v-model="queryParams.payoutFrom"
          placeholder="请输入收款单位"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收款人" prop="payoutUser">
        <el-input
          v-model="queryParams.payoutUser"
          placeholder="请输入收款人"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="payoutPhone">
        <el-input
          v-model="queryParams.payoutPhone"
          placeholder="请输入联系电话"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="Plus"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['web:PayoutInformation:add']"-->
<!--          >新增</el-button-->
<!--        >-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="Edit"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['web:PayoutInformation:edit']"-->
<!--          >修改</el-button-->
<!--        >-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="Delete"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['web:PayoutInformation:remove']"-->
<!--          >删除</el-button-->
<!--        >-->
<!--      </el-col>-->
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="PayoutInformationList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="收款单位" width="250" align="center" prop="payoutFrom" />
<!--      <el-table-column label="收款人" align="center" prop="payoutUser" />-->
<!--      <el-table-column label="联系电话" align="center" prop="payoutPhone" />-->
      <el-table-column label="收款金额" align="center" prop="payoutPrice" />
      <el-table-column label="确认收款时间" align="center" prop="confirmationPaymentTime" />
      <el-table-column label="收款方式" align="center" prop="paymentMethod">
        <template #default="scope">
          <span>{{payList.find(r=>r.name == scope.row.paymentMethod) ? payList.find(r=>r.name == scope.row.paymentMethod).value : '未知'}}</span>
        </template>
      </el-table-column>
<!--      <el-table-column-->
<!--        label="收款码"-->
<!--        align="center"-->
<!--        prop="payoutCode"-->
<!--        width="100"-->
<!--      >-->
<!--        <template #default="scope">-->
<!--          <image-preview :src="scope.row.payoutCode" :width="50" :height="50" />-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column
        label="主键"
        align="center"
        prop="payoutKey"
        v-if="false"
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
<!--          <el-button-->
<!--            type="text"-->
<!--            icon="Edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['web:PayoutInformation:edit']"-->
<!--            >修改</el-button>-->
<!--          <el-button-->
<!--            type="text"-->
<!--            icon="Edit"-->
<!--            @click="handleConfirm(scope.row)"-->
<!--            v-hasPermi="['web:PayoutInformation:edit']"-->
<!--          >确认收款</el-button>-->
          <el-button
            type="text"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['web:PayoutInformation:remove']"
            >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改收款信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form
        ref="PayoutInformationRef"
        :model="form"
        :rules="rules"
        label-width="80px"
      >
        <el-form-item label="收款单位" prop="payoutFrom">
          <el-input v-model="form.payoutFrom" placeholder="请输入收款单位" />
        </el-form-item>
        <el-form-item label="收款人" prop="payoutUser">
          <el-input v-model="form.payoutUser" placeholder="请输入收款人" />
        </el-form-item>
        <el-form-item label="联系电话" prop="payoutPhone">
          <el-input v-model="form.payoutPhone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="收款金额(元)" prop="payoutPrice">
          <el-input-number v-model="form.payoutPrice" placeholder="请输入收款金额" />
        </el-form-item>
        <el-form-item label="收款码">
          <image-upload limit="1" v-model="form.payoutCode" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm"
            >确 定</el-button
          >
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script name="PayoutInformation">
import {
  listPayoutInformation,
  getPayoutInformation,
  delPayoutInformation,
  addPayoutInformation,
  updatePayoutInformation, confirmReceipt
} from '@/api/web/PayoutInformation'
import { getUserProfile } from '@/api/system/user'

export default {
  name: "PayoutInformation",
  components: {},
  dicts: ['sys_flag', 'sys_fp'],
  data() {
    return {
      multiple: true,
      payList: [{
        value: '现场支付',
        name: 2,
      },{
        value: '微信',
        name: 0,
      },{
        value: '支付宝',
        name: 1,
      },{
        value: '转账',
        name: 3,
      }],
      PayoutInformationList: [],
      open: false,
      buttonLoading: false,
      loading: true,
      showSearch: true,
      ids: [],
      single: true,
      total: 0,
      title: '',
      form: {},
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        payoutFrom: undefined,
        payoutUser: undefined,
        payoutPhone: undefined,
        payoutPrice: undefined,
        payoutCode: undefined,
      },
      rules: {
        payoutFrom: [
          { required: true, message: "收款单位不能为空", trigger: "blur" },
        ],
        payoutUser: [
          { required: true, message: "收款人不能为空", trigger: "blur" },
        ],
        payoutPhone: [
          { required: true, message: "联系人不能为空", trigger: "blur" },
        ],
        payoutPrice: [
          { required: true, message: "收款金额不能为空", trigger: "blur" },
        ],
        payoutCode: [
          { required: true, message: "收款码不能为空", trigger: "blur" },
        ],
        payoutKey: [{ required: true, message: "主键不能为空", trigger: "blur" }],
      },
    }
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询收款信息列表 */
    getList() {
      this.loading = true;
      listPayoutInformation(this.queryParams).then((response) => {
        this.PayoutInformationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        payoutFrom: null,
        payoutUser: null,
        payoutPhone: null,
        payoutPrice: null,
        payoutCode: null,
        payoutKey: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("PayoutInformationRef");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryRef");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.payoutKey);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加收款信息";
    },
    // 确认收款
    handleConfirm(row) {
      getUserProfile().then(response => {
        let params = {
          loginmanId: response.data.user.userId,
          customer_key: row.payoutKey
        }
        confirmReceipt(params).then((response) => {
          console.log(response)
        }).then(e=>{
          // this.$modal.msgError(e.resultMsg)
        });
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const payoutKey = row.payoutKey || this.ids;
      getPayoutInformation(payoutKey).then((response) => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改收款信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["PayoutInformationRef"].validate((valid) => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.payoutKey != null) {
            updatePayoutInformation(this.form)
              .then((response) => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          } else {
            addPayoutInformation(this.form)
              .then((response) => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const payoutKeys = row.payoutKey || ids.value;
      this.$modal
        .confirm('是否确认删除收款信息编号为"' + payoutKeys + '"的数据项？')
        .then(function () {
          this.loading = true;
          return delPayoutInformation(payoutKeys);
        })
        .then(() => {
          this.loading = true;
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .finally(() => {
          this.loading = false;
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "web/PayoutInformation/export",
        {
          ...queryParams.value,
        },
        `PayoutInformation_${new Date().getTime()}.xlsx`
      );
    }
  }
}
</script>
